package com.itwn.portal.web;

import com.itwn.portal.entity.Score;
import com.itwn.portal.entity.User;
import com.itwn.portal.reult.ResponseResult;
import com.itwn.portal.service.ScoreService;
import com.itwn.portal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cx
 * @date 2022/3/1
 * @apiNote
 */
@RestController
@RequestMapping("score")
public class ScoreController {

    @Autowired
    ScoreService scoreService;
    @Autowired
    UserService userService;

    /**
     * 查询资源的平均分
     * @param rid
     * @return
     */
    @RequestMapping("/getAverageById")
    @ResponseBody
    public Double queryAvgById( Integer rid){
        System.out.println(rid+"==========");
        return this.scoreService.queryAvgById(rid);
    }


    @RequestMapping("/getMyScore")
    @ResponseBody
    public double queryMyScore(Integer rid, Integer uid){
        System.out.println(rid+"=========="+uid);
        if(uid>0){
            double a = this.scoreService.queryMyScore(rid,uid);
            System.out.println("为什么我的评分是："+a);
            return a;
        }else{
            return 0;
        }
    }

    @RequestMapping("/getVoteNum")
    @ResponseBody
    public int queryVoteNum(Integer rid){
        return this.scoreService.selectVoteNum(rid);
    }

    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Score score){
        System.out.println("执行打分！！！");
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.scoreService.add(score);
            result.setCode(200);
            result.setMsg("保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return result;
    }



}


